package com.hollycrm.hollysqm.core.chat.parsing;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import com.hollycrm.hollysqm.core.chat.bean.ParseResult;
import com.hollycrm.hollysqm.core.chat.bean.Record;
import com.hollycrm.hollysqm.core.chat.bean.Root;
import com.hollycrm.hollysqm.core.chat.bean.Sentence;
import com.hollycrm.hollysqm.core.chat.bean.SentenceList;
import com.hollycrm.hollysqm.core.chat.bean.Silence;
import com.hollycrm.hollysqm.core.chat.bean.SilenceList;

public class ChatParser {

	private final static String CHAT_PACKAGE = "com.hollycrm.hollysqm.core.chat.bean";
	
	private static ChatParser chatParser = null;
	
	public static ChatParser getInstance(){
		if (chatParser == null){
			synchronized(ChatParser.class){
				return new ChatParser();
			}
		}
		return chatParser;
	}
	
	/**
	 * XML解析
	 * @param filePath 文件路径
	 * @return
	 * @throws Exception
	 */
	public ParseResult xmlParser(String filePath) throws Exception {
		return this.xmlParser(new File(filePath));		
	}
	
	/**
	 * XML解析
	 * @param  file 文件
	 * @return
	 * @throws Exception
	 */
	public ParseResult xmlParser(File file) throws Exception {
		return this.xmlParser(new FileInputStream(file));
	}
	
	/**
	 * XML解析
	 * @param is 文件流
	 * @return
	 * @throws Exception
	 */
	public ParseResult xmlParser(InputStream is) throws Exception {
		JAXBContext jc = JAXBContext.newInstance(CHAT_PACKAGE); 
//		JAXBContext jc = JAXBContext.newInstance(Root.class); 
		Unmarshaller us = jc.createUnmarshaller();//Unmarshaller接口，将XML数据反序列化为Java对象
//		Marshaller mr = jc.createMarshaller();//Marshaller接口，将Java对象序列化为XML数据
//		mr.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
//		mr.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); 
		Root r = (Root)us.unmarshal(is);
		ParseResult result = new ParseResult();
		Record record = r.getRecord();		
		result.setRecordKey(record.getRecordKey());//主键ID
		result.setCreateTime(record.getCreateTime() + "");//创建时间
		SilenceList silenceList = record.getSilenceList();
		result.setTotalTime(silenceList.getTotalTime().longValue());//总时长
		List<Silence> silence=silenceList.getSilence();
		Silence sil = silence.get(0);
		result.setStartTime(sil.getStartTime() + "");//开始时间
		result.setEndTime(sil.getEndTime() + "");//结束时间
		SentenceList sentenceList = record.getSentenceList();
		List<Sentence> sentence = sentenceList.getSentence();
		long role = 0;		
		String allText = "";
		String userText = "";
		String agentText = "";
		if (sentence != null && sentence.size()>0) {
			String text  = "";
			for (Sentence s : sentence){
				role = s.getRole().longValue();
				text = s.getText();
				if (role == 1){
					userText += String.format("%s|", text);
					text = String.format("n1#%s;", text);//客户
				}else if (role == 2) {
					agentText += String.format("%s|", text);
					text = String.format("n0#%s;", text);//客服
				}
				allText += text;
			}
		}
		result.setUserText(userText);//用户文本
		result.setAgentText(agentText);//坐席文本
		result.setAllText(allText);//所有文本
		return result;
	}
	
	public static void main(String[] args) throws Exception{
		File file = new File("D:\\Workspaces\\Eclipse-SR5\\hollysqm\\hollysqm-core\\src\\main\\resources\\I8_chat.xml");
		InputStream tempIs = new FileInputStream(file);
		ParseResult result = ChatParser.getInstance().xmlParser(tempIs);
		System.out.println(result.getUserText());
		System.out.println(result.getAgentText());
		System.out.println(result.getAllText());
	}
	
}
